package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import data.GroupSystem;

/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class contains and manages all the ViewPanels.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */
public class ViewManager
{
    private ArrayList<ViewPanel> views;
    private HomeFrame home;
    private GroupSystem gs;
    
    /**
     * Constructs ViewManger the manages all the ViewPanels
     * @param gs the GroupSystem specific to the ViewManager 
     */
    public ViewManager(GroupSystem gs)
    {
        this.gs = gs;
        views = new ArrayList<ViewPanel>();
        home = new HomeFrame(this);
        this.addView(new CalendarPanel(this, "calendar"));
        this.addView(new DocumentPanel(this, "document"));
        this.addView(new AddDocument(this, "add document"));
        this.addView(new EditProfile(this, "edit profile"));
        this.addView(new ChangePassword(this, "change password"));
        this.addView(new EditGroup(this, "edit group"));
        this.addView(new AddEvent(this, "add event"));
        this.addView(new AddDeadline(this, "add deadline"));
        this.addView(new AddMeeting(this, "add meeting"));
        this.addView(new AddMember(this, "add member"));
        this.addView(new EmailPanel(this, "email"));
        this.addView(new ShowEvent(this, "show event"));
        
        this.showView("calendar");
    }
    
    /**
     * Adds a new view to the ViewManager
     * @param view the new view to be added.
     */
    public void addView(ViewPanel view)
    {
        home.getViewPanel().add(view);
        view.setVisible(false);
        views.add(view);
    }
    
    /**
     * Displays the specified view and hides all other view panels
     * @param view the view's identification
     */
    public void showView(String view)
    {
        for (ViewPanel panel : views)
        {
            if (panel.getID().equals(view))
            {
                panel.setVisible(true);
                panel.clearView();
            }
            else
                panel.setVisible(false);
        }
    }
    
    /**
     * Returns the specified ViewPanel 
     * @param view the view's identification
     * @return the specified ViewPanel. If not found it returns null
     */
    public ViewPanel getView(String view)
    {
        for (ViewPanel panel : views)
        {
            if (panel.getID().equals(view))
                return panel;
        }
        return null;
    }
    
    /**
     * Sets a link to the specified ViewPanel to be displayed
     * @param id the view's specified identification
     * @return the actionListener that changes ViewPanels
     */
    public ActionListener addLink(final String id)
    {
        return new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                showView(id);
                home.repaint();
            }
            
        };
    }

    /**
     * Returns the GroupSystem specific to the ViewManager
     * @return the GroupSystem specific to the ViewManager
     */
    public GroupSystem getGs()
    {
        return gs;
    }
}
